﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx?lb=审批");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx?lb=审批");
        }
        if (Session["pn"].ToString().Contains("设备科") == false)
        {
            Response.Redirect("default.aspx?lb=审批");
        }
        Label1.Text = Session["pn"].ToString();
        Label2.Text = Session["pngeren"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        double heji=0;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            string txt1 = ((TextBox)GridView1.Rows[i].Cells[10].FindControl("TextBox2")).Text;
            if (txt1.Length == 0)
            {
                string jinggao1 = "<SCRIPT language=javascript>alert('";
                jinggao1 += "请输入序号为 " + (GridView1.PageSize * GridView1.PageIndex + i + 1).ToString() + " 的材料数量！";
                jinggao1 += "');</SCRIPT>";
                ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao1);

                //ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao1, true);
                return;
            }
            for (int j = 0; j < txt1.Trim().Length; j++)
            {
                char a;
                int k;
                a = Convert.ToChar(txt1.Substring(j, 1));
                k = (int)a;
                if (k < 48 || k > 57)
                {
                    string jinggao2 = "<SCRIPT language=javascript>alert('";
                    jinggao2 += "请在序号为 " + (GridView1.PageSize * GridView1.PageIndex + i + 1).ToString() + " 的材料数量中输入数字！";
                    jinggao2 += "');</SCRIPT>";
                    ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao2);

                    //ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao2, true);

                    return;
                }
            }
            heji += Convert.ToInt32(txt1) * Convert.ToDouble(GridView1.Rows[i].Cells[8].Text.ToString());

        }

        
        if (RequiredFieldValidator1.IsValid==false)
        {
            string jinggao2 = "<SCRIPT language=javascript>alert('";
            jinggao2 += "请输入列支情况！";
            jinggao2 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa2", jinggao2);
            TextBox4.Focus();
        }
        else
        {
            string jinggao4 = "<SCRIPT language=javascript>alert('";
            jinggao4 += "该计划单此次审批总计" + heji.ToString() + "元！";
            jinggao4 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa2", jinggao4);


            try
            {
                //更新明细表
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {

                    int id = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);
                    string cjsl = ((TextBox)GridView1.Rows[i].Cells[11].FindControl("TextBox2")).Text;
                    string beizhu = ((TextBox)GridView1.Rows[i].Cells[12].FindControl("TextBox3")).Text;
                    string sqlstr = "update cailiaomingxi set duanshuliang='" + cjsl + "', beizhu='" + beizhu + "' where id=" + id;
                    //Response.Write(sqlstr);
                    //Response.End();
                    sqlcon = new SqlConnection(strCon);
                    sqlcom = new SqlCommand(sqlstr, sqlcon);
                    sqlcon.Open();
                    sqlcom.ExecuteNonQuery();
                    sqlcon.Close();
                }
                //更新管理表
                string sqlstr2 = "update cailiaoguanli set duanname='" + Label2.Text + "',duantime='" + DateTime.Now.ToString() + "',duanip='" + Page.Request.UserHostAddress.ToString().Trim() + "',liezhi='" + TextBox4.Text + "',zt=4 where c_id=" + Request.QueryString["id"];
                //Response.Write(sqlstr2);
                //Response.End();
                sqlcon = new SqlConnection(strCon);
                sqlcom = new SqlCommand(sqlstr2, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();

            }
            catch (SqlException)
            {
                throw;
            }

            string jinggao3 = "<SCRIPT language=javascript>alert('";
            jinggao3 += "审批操作成功！";
            jinggao3 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
            ClientScript.RegisterStartupScript(this.GetType(), "aa4", "<SCRIPT language=javascript>document.location.href='sbk_shenpilist.aspx';</SCRIPT>");
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
        double heji = 0;
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[9].Text = "车间审批总计";
            for (int j = 0; j <= GridView1.Rows.Count - 1; j++)
            {
                GridViewRow row = this.GridView1.Rows[j];
                heji += Convert.ToInt32(row.Cells[10].Text.ToString()) * Convert.ToDouble(row.Cells[8].Text.ToString());
            }
            e.Row.Cells[10].Text = heji.ToString() + "元";
            e.Row.Cells[10].ForeColor = System.Drawing.Color.Red;
        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
            e.Row.Cells[6].Text = e.Row.Cells[6].Text.Replace("&amp;#215;", "×").Replace("&amp;quot;", "");
        }

    }

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }
    }
}